##
## Picon renderer by Gruffy .. some speedups by Ghost, powered by cmikula
##
from Renderer import Renderer
from enigma import ePixmap, eEnv
from Tools.Directories import fileExists, SCOPE_SKIN_IMAGE, SCOPE_CURRENT_SKIN, resolveFilename
from re import compile
from enigma import eSize
from enigma import ePicLoad
from Components.AVSwitch import AVSwitch

class extPicon(Renderer):
    searchPaths = (eEnv.resolve('${datadir}/enigma2/%s/'),
                '/media/cf/%s/',
                '/media/usb/%s/')

    def __init__(self):
        print "extPicon"
        Renderer.__init__(self)
        self.path = "picon"
        self.pic_size = ""
        self.nameCache = { }
        self.pngname = ""
        self.partnerbox = compile('1:0:[0-9a-fA-F]+:[1-9a-fA-F]+[0-9a-fA-F]*:[1-9a-fA-F]+[0-9a-fA-F]*:[1-9a-fA-F]+[0-9a-fA-F]*:[1-9a-fA-F]+[0-9a-fA-F]*:[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+:http')

    def applySkin(self, desktop, parent):
        attribs = [ ]
        for (attrib, value) in self.skinAttributes:
            if attrib == "size":
                self.pic_size = self.parseSize(value)
            if attrib == "path":
                self.path = value
            else:
                attribs.append((attrib,value))
        self.skinAttributes = attribs
        return Renderer.applySkin(self, desktop, parent)

    GUI_WIDGET = ePixmap

    def parseSize(self, str):
        x, y = str.split(',')
        return (int(x), int(y))

    def changed(self, what):
        if self.instance:
            pngname = ""
            if what[0] != self.CHANGED_CLEAR:
                sname = self.source.text
                pos = sname.rfind(':')
                pos2 = sname.rfind(':', 0, pos)
                if pos - pos2 == 1 or self.partnerbox.match(sname) is not None:
                    sname = sname[:pos2].replace(':', '_')
                else:
                    sname = sname[:pos].replace(':', '_')
                pngname = self.nameCache.get(sname, "")
                if pngname == "":
                    pngname = self.findPicon(sname)
                    if pngname != "":
                        self.nameCache[sname] = pngname
            if pngname == "": # no picon for service found
                pngname = self.nameCache.get("default", "")
                if pngname == "": # no default yet in cache..
                    pngname = self.findPicon("picon_default")
                    if pngname == "":
                        tmp = resolveFilename(SCOPE_CURRENT_SKIN, "picon_default.png")
                        if fileExists(tmp):
                            pngname = tmp
                        else:
                            pngname = resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/picon_default.png")
                    self.nameCache["default"] = pngname
            if self.pngname != pngname:
                print "extPic load", str(pngname)
                picload = ePicLoad()
                sc = AVSwitch().getFramebufferScale()
                picload.setPara((self.instance.size().width(), self.instance.size().height(), sc[0], sc[1], False, 1, "#ff000000"))
                picload.startDecode(pngname, 0, 0, False)
                data = picload.getData()
                self.instance.setPixmap(data)
                del picload
                self.pngname = pngname

    def findPicon(self, serviceName):
        for path in self.searchPaths:
            pngname = (path % self.path) + serviceName + ".png"
            if fileExists(pngname):
                return pngname
        return ""
